package com.tian.service.impl;

import com.tian.client.MessageUseCouponFailedClient;
import com.tian.common.CommonResult;
import com.tian.dto.OrderUseCouponDto;
import com.tian.entity.ChargeUserCoupon;
import com.tian.entity.MessageUseCouponFailed;
import com.tian.enums.ResultCode;
import com.tian.enums.UserCouponStatusEnum;
import com.tian.mapper.ChargeUserCouponMapper;
import com.tian.mapper.MessageUseCouponFailedMapper;
import com.tian.service.MessageUseCouponFailedJobService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-03 19:37
 * {@code @version:} 1.0
 */
@Service
public class MessageUseCouponFailedJobServiceImpl implements MessageUseCouponFailedJobService {
    @Resource
    private MessageUseCouponFailedMapper messageUseCouponFailedMapper;
    @Resource
    private MessageUseCouponFailedClient messageUseCouponFailedClient;
    @Resource
    private ChargeUserCouponMapper chargeUserCouponMapper;
    @Resource
    private RedissonClient redissonClient;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void process() {
        // 查询状态为待处理的消息
        List<MessageUseCouponFailed> messageUseCouponFailedList = messageUseCouponFailedMapper.selectByStatus();
        // 遍历处理
        for (MessageUseCouponFailed messageUseCouponFailed : messageUseCouponFailedList) {
            Long orderId = messageUseCouponFailed.getOrderId();
            //查询支付服务，获取订单的优惠券信息
            CommonResult<List<OrderUseCouponDto>> commonResult = messageUseCouponFailedClient.query(orderId);
            if (commonResult.getCode() == ResultCode.SUCCESS.getCode()) {
                List<OrderUseCouponDto> data = commonResult.getData();
                // 遍历优惠券信息，更新用户优惠券状态
                for (OrderUseCouponDto orderUseCouponDto : data) {
                    RLock lock = redissonClient.getLock("update_user_coupon_status:"  + orderUseCouponDto.getUserCouponId());
                    lock.lock();
                    try {
                        ChargeUserCoupon userCoupon = chargeUserCouponMapper.selectByPrimaryKey(orderUseCouponDto.getUserCouponId());
                        if (userCoupon.getStatus() == UserCouponStatusEnum.INIT.getStatus()) {
                            userCoupon.setUseTime(messageUseCouponFailed.getUseTime());
                            userCoupon.setUseOrderNo(orderUseCouponDto.getOrderId().toString());
                            userCoupon.setStatus(orderUseCouponDto.getStatus());
                            chargeUserCouponMapper.updateByPrimaryKey(userCoupon);
                        }
                        // 更新状态为已处理
                        messageUseCouponFailed.setStatus(1);
                        messageUseCouponFailedMapper.updateByPrimaryKey(messageUseCouponFailed);
                    }finally {
                        lock.unlock();
                    }
                }
            }
        }
    }
}
